package com.fancy.learncenter.common;


import com.fancy.learncenter.utils.LogUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;

/**
 * 签名校验方法封装
 * <p>
 * Created by shoucai on 16/11/8.
 * Date : 16/11/8
 * Time : 22:32
 */
public class SignVerify {


    /**
     * @return true-校验通过 false-校验失败
     * @Title: SignVerify.java
     * @Package com.tulanduoart.teeya.verify
     * @Description: 签证签名
     * @author: shoucai.wang
     * @date: 16/11/9 10:45
     */
    public static Boolean verifySign(Map<String, Object> map) {

        String requestSign = (String) map.get(Constant.SIGN_NAME_HEADER);
        System.out.println("request sign=" + requestSign);
        String signSelf = SignVerify.getSign(map);
        if (!requestSign.equals(signSelf)) {
            return Boolean.FALSE;
        }
        return Boolean.TRUE;
    }

    /**
     * @Title: SignVerify.java
     * @Package com.tulanduoart.teeya.verify
     * @Description: 获取签名
     * @author: shoucai.wang
     * @date: 16/11/9 10:45
     */
    public static String getSign(Map<String, Object> map) {
        ArrayList<String> list = new ArrayList<String>();
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            if (entry.getValue() != "") {
                String fn = entry.getKey();
                if (!Constant.SIGN_NAME_HEADER.equals(fn)) {
                    list.add(entry.getKey() + "=" + entry.getValue() + "&");
                }
            }
        }
        int size = list.size();
        String[] arrayToSort = list.toArray(new String[size]);
        Arrays.sort(arrayToSort, String.CASE_INSENSITIVE_ORDER);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < size; i++) {
            sb.append(arrayToSort[i]);
        }
        String result = sb.toString();
        System.out.println(result);
        LogUtil.MyLog("RSA", "====SAFSAF=====" + result);
//        result += "key=" + Constant.APP_SECRET_TEEYA;
//        result = MD5.hexdigest(result).toUpperCase();
        return result;
    }
}
